home *** CD-ROM | disk | FTP | other *** search
- /*
- * Blob Manager Demonstration: Magic Square
- *
- * All rows, columns and diagonals of a square must add up to 15.
- *
- * This is a donorless scenario. The receptors are created, then glued
- * to themselves initially so that they will each have a glob to drag
- * around.
- *
- * 26 July 1986 Paul DuBois
- */
-
- # include "TransSkel.h"
-
- # include "BlobMgr.h"
- # include "BlobDemo.h"
-
-
- # define vButton 5
- # define vBoard 30 /* offset of top of board */
- # define hGap 2
- # define vGap 2
- # define bSqSize 30 /* board square size */
-
-
- static WindowPtr wind;
- static BlobSetHandle receptors;
-
- static Boolean wait;
- static ControlHandle button;
- static short hMid;
- static short hBoard;
-
-
- /*
- * Make receptor blob
- */
-
- static void
- MakeRBlob (Rect *r)
- {
- BlobHandle b;
- char c;
- static long rno = 0L;
-
- b = NewBlob (receptors, true, infiniteGlue, false, ++rno);
- OpenBlob ();
- EraseRect (r);
- FrameRect (r);
- InsetRect (r, 5, 7);
- c = rno + '0';
- TextBox (&c, 1L, r, teJustCenter);
- InsetRect (r, -3, -5);
- InvertRect (r);
- InsetRect (r, -2, -2);
- CloseRectBlob (b, r, r);
- GlueGlob (b, b);
- }
-
-
- static void
- MakeReceptors (void)
- {
- receptors = NewBlobSet ();
- MakeBlobGrid (3, 3, hBoard, vBoard, bSqSize, bSqSize,
- hGap, vGap, MakeRBlob);
- }
-
-
- static void
- Restart (void)
- {
-
- HiliteControl (button, dimHilite);
- ShuffleGlobSet (receptors);
- wait = false;
- }
-
-
- static Boolean
- TestConfig (short pos1, short pos2, short pos3)
- {
- short sum;
-
- sum = GetBRefCon (BGlob (GetBlobHandle (receptors, pos1)))
- + GetBRefCon (BGlob (GetBlobHandle (receptors, pos2)))
- + GetBRefCon (BGlob (GetBlobHandle (receptors, pos3)));
- return (sum == 15);
- }
-
-
- static void
- CheckStatus (void)
- {
- if (TestConfig (0, 1, 2) /* top row */
- && TestConfig (3, 4, 5) /* middle row */
- && TestConfig (6, 7, 8) /* bottom row */
- && TestConfig (0, 3, 6) /* left column */
- && TestConfig (1, 4, 7) /* middle column */
- && TestConfig (2, 5, 8) /* right column */
- && TestConfig (0, 4, 8) /* diagonal */
- && TestConfig (2, 4, 6) ) /* diagonal */
- {
- HiliteControl (button, normalHilite);
- wait = true;
- }
- }
-
-
- static pascal void
- Mouse (Point pt, long t, short mods)
- {
- BlobHandle b, d;
- ControlHandle ctl;
-
- if (FindControl (pt, wind, &ctl))
- {
- if (TrackControl (ctl, pt, nil))
- Restart ();
- }
- else if (!wait)
- BlobClick (pt, t, nil, receptors);
- /*
- * Check status even if user just restarted, since the square may
- * by chance fall into a correct configuration. In that case,
- * the CheckStatus will again enable the restart button.
- */
- CheckStatus ();
- }
-
-
- static pascal void
- Update (Boolean resized)
- {
- DrawControls (wind);
- DrawBlobSet (receptors);
- }
-
-
- static pascal void
- Activate (Boolean active)
- {
- if (active)
- {
- SetDragRects (wind);
- SetBCPermissions (false, false, false, true, true);
- }
- }
-
-
- void
- MagicInit (void)
- {
- Rect r;
-
- SkelWindow (wind = GetDemoWind (magicWindRes),
- Mouse, /* mouse clicks */
- nil, /* key clicks */
- Update, /* updates */
- Activate, /* activate/deactivate events */
- nil, /* close window */
- DoWClobber, /* dispose of window */
- nil, /* idle proc */
- false); /* irrelevant, since no idle proc */
-
- hMid = wind->portRect.right / 2;
- hBoard = hMid - (3 * (bSqSize + hGap) - hGap) / 2;
- SetRect (&r, hMid - 40, vButton, hMid + 40, vButton + 20);
- button = NewControl (wind, &r, "\pReset", true, 0, 0, 0,
- pushButProc, 0L);
-
- MakeReceptors ();
-
- MakeFrontWind (wind); /* show window */
- Restart (); /* scramble it */
- SkelDoUpdates (); /* process update resulting from scramble */
- }
-